Skip to content

Conversation

@BadMooncc
Copy link

@BadMooncc BadMooncc commented Nov 17, 2025

English Template / 英文模板

🤔 这个变动的性质是?

  • 🆕 新特性提交
  • 🐞 Bug 修复
  • 📝 站点、文档改进
  • 📽️ 演示代码改进
  • 💄 组件样式/交互改进
  • 🤖 TypeScript 定义更新
  • 📦 包体积优化
  • ⚡️ 性能优化
  • ⭐️ 功能增强
  • 🌐 国际化改进
  • 🛠 重构
  • 🎨 代码风格优化
  • ✅ 测试用例
  • 🔀 分支合并
  • ⏩ 工作流程
  • ⌨️ 无障碍改进
  • ❓ 其他改动(是关于什么的改动?)

🔗 相关 Issue

  1. 描述相关需求的来源,如相关的 issue 讨论链接。
  2. close #50885

💡 需求背景和解决方案

  1. inputNumber新增allowClear功能。
  2. allowClear: boolean | { clearValue: number | string } ;onClear: () => void。
  3. image

📝 更新日志

语言 更新描述
🇺🇸 英文 The InputNumber component supports the allowClear and onClear methods.
🇨🇳 中文 InputNumber支持allowClear和onClear方法

Summary by CodeRabbit

发布说明

  • 新功能

    • InputNumber 组件新增清除功能,可通过 allowClear 启用,支持自定义清除后值(clearValue),并新增 onClear 事件回调。
  • 样式

    • 为清除按钮及其隐藏态添加了样式,改善显示与交互一致性。
  • 文档

    • API 文档补充了 allowClear 与 onClear 的说明,并新增示例演示用法。
  • 测试

    • 新增 allowClear 相关测试,覆盖显示、交互、清除行为与事件触发。

@vercel
Copy link

vercel bot commented Nov 17, 2025

@BadMooncc is attempting to deploy a commit to the React Component Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link

coderabbitai bot commented Nov 17, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

为 InputNumber 增加 allowClear 清空功能:新增组件属性与回调、清空按钮 UI 与样式、示例、文档与测试,点击清空时通过 allowClear.clearValue 或 undefined 更新值并触发 onClear。(≤50 字)

Changes

内聚体 / 文件(s) 变更摘要
样式扩展
assets/index.less
添加 .rc-input-number-clear-icon.rc-input-number-clear-icon-hidden 两个 CSS 类,用于清空按钮的基础样式与隐藏状态。
API 文档更新
docs/api.md
在 InputNumber API 中新增 `allowClear: boolean
演示代码
docs/demo/allow-clear.tsx
新增演示文件,展示带前缀 "¥"、后缀 "RMB" 与 allowClear 配置的 InputNumber 示例。
文档索引
docs/example.md
在文档示例中插入 "allow-clear" 小节并引用 ./demo/allow-clear.tsx 示例。
核心实现
src/InputNumber.tsx
在组件 props 中新增 allowClearonClear,渲染清空按钮并在点击时读取 allowClear.clearValue(或使用 undefined)调用 triggerValueUpdate,并触发 onClear;处理 disabled/readOnly 与 suffix 的显示逻辑。
功能测试
tests/allowClear.test.tsx
新增测试套件,覆盖清空图标渲染/隐藏、点击清空行为(默认/null/指定 clearValue/0/字符串模式)、onChangeonClear 触发、后缀交互及 defaultValue 场景。

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant UI as InputNumber UI
    participant Comp as InputNumber Component
    participant Props as Props Handler

    User->>UI: 点击清空按钮
    activate UI
    UI->>Comp: onClear 按钮点击事件
    activate Comp

    alt 有 onClear 回调
        Comp->>Props: 调用 onClear()
        Note right of Props `#D3F9D8`: 可选回调触发
    end

    Comp->>Comp: 读取 allowClear.clearValue 或 使用 undefined
    Comp->>Comp: 调用 triggerValueUpdate(clearValue)
    Comp->>UI: 更新显示值(空或指定值)

    deactivate Comp
    deactivate UI
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 分钟

  • 重点检查:
    • src/InputNumber.tsx 中调用 triggerValueUpdate 的边界条件与与现有更新流程的兼容性。
    • 测试对 0、undefined、字符串模式和 defaultValue 的断言是否充分且无歧义。
    • 新增 CSS 类在不同样式上下文(有 suffix / disabled / readOnly)下的布局表现与命名一致性。

Suggested reviewers

  • zombieJ

Poem

🐰 轻触一符去尘踪,
数字归空月光中,
allowClear 随意放,onClear 轻声问,
测试为伴路更稳,
小红点下笑声融 ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 拉取请求标题明确描述了主要更改:向 InputNumber 组件添加 allowClear 功能支持,与整个变更集的核心目标完全相关。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fb8f5ea and a92d799.

📒 Files selected for processing (6)
  • assets/index.less (1 hunks)
  • docs/api.md (2 hunks)
  • docs/demo/allow-clear.tsx (1 hunks)
  • docs/example.md (1 hunks)
  • src/InputNumber.tsx (5 hunks)
  • tests/allowClear.test.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (5)
  • src/InputNumber.tsx
  • tests/allowClear.test.tsx
  • assets/index.less
  • docs/api.md
  • docs/demo/allow-clear.tsx
🔇 Additional comments (1)
docs/example.md (1)

16-18: 文档结构保持一致。

新增的 allow-clear 示例章节遵循现有文档格式规范,与其他示例条目保持一致,引用路径正确。

Tip

📝 Customizable high-level summaries are now available in beta!

You can now customize how CodeRabbit generates the high-level summary in your pull requests — including its content, structure, tone, and formatting.

  • Provide your own instructions using the high_level_summary_instructions setting.
  • Format the summary however you like (bullet lists, tables, multi-section layouts, contributor stats, etc.).
  • Use high_level_summary_in_walkthrough to move the summary from the description to the walkthrough section.

Example instruction:

"Divide the high-level summary into five sections:

  1. 📝 Description — Summarize the main change in 50–60 words, explaining what was done.
  2. 📓 References — List relevant issues, discussions, documentation, or related PRs.
  3. 📦 Dependencies & Requirements — Mention any new/updated dependencies, environment variable changes, or configuration updates.
  4. 📊 Contributor Summary — Include a Markdown table showing contributions:
    | Contributor | Lines Added | Lines Removed | Files Changed |
  5. ✔️ Additional Notes — Add any extra reviewer context.
    Keep each section concise (under 200 words) and use bullet or numbered lists for clarity."

Note: This feature is currently in beta for Pro-tier users, and pricing will be announced later.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @BadMooncc, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a new allowClear feature to the InputNumber component, significantly improving user experience by adding a clear button to reset the input's value. The feature is highly configurable, allowing for custom clear values and providing an onClear callback for additional control. The implementation includes updates to the component's core logic, styling, comprehensive documentation, a new interactive demo, and a robust suite of unit tests to ensure reliability.

Highlights

  • New Feature: allowClear Prop: The InputNumber component now supports an allowClear prop, enabling users to easily clear the input's current value. This enhances usability by providing a quick way to reset the input.
  • Customizable Clear Value: The allowClear prop can be configured as a boolean (clearing to null) or an object { clearValue: number | string }, allowing developers to specify a custom value that the input should reset to when the clear button is clicked.
  • onClear Callback: A new onClear callback prop has been introduced. This function is triggered when the user clicks the clear button, providing a convenient hook for executing custom logic or side effects upon clearing the input.
  • Styling and Conditional Visibility: Dedicated CSS classes (-clear-icon, -clear-icon-hidden, -clear-icon-has-suffix) have been added to style the clear button. Its visibility is dynamically managed, appearing only when the input has a value and is not disabled or read-only, and adjusting its position if a suffix is present.
  • Comprehensive Testing and Documentation: Extensive unit tests have been added to cover various scenarios for the allowClear functionality, including different clearValue types, disabled/readOnly states, and onClear callback invocation. The API documentation and a new demo have also been updated to reflect these changes.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

感谢你的贡献!这个 PR 为 InputNumber 组件增加了 allowClear 功能,这是一个很棒的增强。整体实现很好,也包含了相应的文档和测试。

我发现了一个关于清除按钮可见性逻辑的小问题,它在值为 0 或在非受控模式下表现不正确。此外,测试用例也缺少了对这些场景的覆盖。

我在代码中留下了一些具体的修改建议,希望能帮助你完善这个功能。

Comment on lines 724 to 728
className={clsx(`${prefixCls}-clear-icon`, {
[`${prefixCls}-clear-icon-hidden`]: !(!disabled && !readOnly && value),
[`${prefixCls}-clear-icon-has-suffix`]: !!suffix,
})}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

清除按钮的可见性逻辑存在问题。当前使用 value prop 来判断,但这会导致在以下两种情况下按钮被错误地隐藏:

  1. InputNumber 的值为 0 时(因为 !0 的结果是 true)。
  2. 当组件为非受控模式时(使用 defaultValue),value prop 为 undefined

建议改用内部状态 decimalValue 来进行判断,它能更准确地反映组件的当前值是否为空。这样可以确保在值为 0 或使用 defaultValue 时,清除按钮也能正确显示。

Suggested change
className={clsx(`${prefixCls}-clear-icon`, {
[`${prefixCls}-clear-icon-hidden`]: !(!disabled && !readOnly && value),
[`${prefixCls}-clear-icon-has-suffix`]: !!suffix,
})}
className={clsx(`${prefixCls}-clear-icon`, {
[`${prefixCls}-clear-icon-hidden`]: disabled || readOnly || decimalValue.isEmpty(),
[`${prefixCls}-clear-icon-has-suffix`]: !!suffix,
})}

const clearIcon = container.querySelector('.rc-input-number-clear-icon');
expect(clearIcon).toBeTruthy();
expect(clearIcon).not.toHaveClass('rc-input-number-clear-icon-hidden');
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

建议在此处增加一个测试用例,用于验证当 value0 时清除图标的可见性。这是一个重要的边界情况,因为 0 是一个假值(falsy value),可能会导致可见性判断出错。

你可以添加如下测试:

it('should render clear icon when value is 0', () => {
  const { container } = render(<InputNumber allowClear value={0} />);
  const clearIcon = container.querySelector('.rc-input-number-clear-icon');
  expect(clearIcon).not.toHaveClass('rc-input-number-clear-icon-hidden');
});

);

const clearIcon = container.querySelector('.rc-input-number-clear-icon');
fireEvent.click(clearIcon);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

这个针对 defaultValue(非受控模式)的测试用例缺少了对清除按钮初始可见性的断言。在点击之前,应该先验证按钮是可见的,以确保它在非受控模式下能正确显示。

Suggested change
fireEvent.click(clearIcon);
expect(clearIcon).not.toHaveClass('rc-input-number-clear-icon-hidden');
fireEvent.click(clearIcon);

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
assets/index.less (1)

97-105: 建议添加鼠标样式以改善用户体验

清除按钮的基础样式正确。为了提升用户体验,建议添加 cursor: pointer 以及悬停状态的视觉反馈。

可选的改进:

   &-clear-icon {
     padding: 0;
     font-size: 12px;
     background: none;
     border: none;
+    cursor: pointer;
+    &:hover {
+      opacity: 0.7;
+    }
   }
tests/allowClear.test.tsx (1)

6-14: 建议增强可见性验证

测试验证了清除图标元素的存在,但没有明确验证它是否可见(即没有 rc-input-number-clear-icon-hidden 类)。

建议添加显式的可见性检查:

   it('should render clear icon when allowClear is true and value is not empty', () => {
     const { container } = render(
       <InputNumber allowClear value={123} />
     );
     
     const clearIcon = container.querySelector('.rc-input-number-clear-icon');
     expect(clearIcon).toBeTruthy();
     expect(clearIcon).not.toHaveClass('rc-input-number-clear-icon-hidden');
+    // Verify icon is visible
+    expect(clearIcon).toBeVisible();
   });
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fdde44f and d39228f.

📒 Files selected for processing (6)
  • assets/index.less (1 hunks)
  • docs/api.md (2 hunks)
  • docs/demo/allow-clear.tsx (1 hunks)
  • docs/example.md (1 hunks)
  • src/InputNumber.tsx (5 hunks)
  • tests/allowClear.test.tsx (1 hunks)
🧰 Additional context used
🧠 Learnings (3)
📚 Learning: 2024-09-29T07:54:50.892Z
Learnt from: bombillazo
Repo: react-component/input-number PR: 644
File: tests/validator.test.tsx:34-83
Timestamp: 2024-09-29T07:54:50.892Z
Learning: In `tests/validator.test.tsx`, the test cases focus on the `validator` function. When the validator condition is not met, the input value should not change. The `formatter` is tested separately in another file.

Applied to files:

  • tests/allowClear.test.tsx
📚 Learning: 2024-09-29T06:18:11.993Z
Learnt from: bombillazo
Repo: react-component/input-number PR: 644
File: src/InputNumber.tsx:173-174
Timestamp: 2024-09-29T06:18:11.993Z
Learning: In `src/InputNumber.tsx`, within the `InternalInputNumber` component, the state variables `prevValueRef` and `inputValueRef` are typed as `string | number` to maintain consistency with existing code.

Applied to files:

  • tests/allowClear.test.tsx
  • docs/demo/allow-clear.tsx
  • src/InputNumber.tsx
  • docs/api.md
📚 Learning: 2024-10-08T21:56:37.546Z
Learnt from: bombillazo
Repo: react-component/input-number PR: 644
File: src/InputNumber.tsx:393-395
Timestamp: 2024-10-08T21:56:37.546Z
Learning: The `InputNumber` component does not use error states or messages; it is designed to prevent the value from updating if validation fails without displaying additional feedback to the user.

Applied to files:

  • docs/demo/allow-clear.tsx
  • src/InputNumber.tsx
🧬 Code graph analysis (1)
src/InputNumber.tsx (1)
docs/demo/allow-clear.tsx (1)
  • value (6-25)
🔇 Additional comments (4)
src/InputNumber.tsx (1)

101-101: 新增的属性类型定义合理

allowClearonClear 属性的类型定义清晰且符合预期。支持布尔值和对象形式的 allowClear 提供了良好的灵活性。

Also applies to: 120-120

docs/example.md (1)

16-18: 文档更新正确

新增的 allow-clear 示例章节格式正确,与其他示例保持一致。

docs/demo/allow-clear.tsx (1)

6-25: 示例代码清晰有效

演示组件正确展示了 allowClear 功能的使用方式,包括自定义 clearValue 以及与 prefix/suffix 的配合使用。

docs/api.md (1)

79-84: API 文档完整准确

新增的 allowClearonClear 属性文档清晰明了,类型定义和描述都准确无误。

Also applies to: 157-162

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
assets/index.less (1)

97-105: 改进清除按钮的交互样式

当前样式缺少交互反馈,建议添加:

  1. 鼠标指针样式
  2. 悬停和焦点状态

应用以下改进:

   &-clear-icon {
     padding: 0;
     font-size: 12px;
     background: none;
     border: none;
+    cursor: pointer;
+    color: rgba(0, 0, 0, 0.45);
+    transition: color 0.3s;
+
+    &:hover {
+      color: rgba(0, 0, 0, 0.85);
+    }
+
+    &:focus {
+      outline: none;
+      color: #1890ff;
+    }
   }
   &-clear-icon-hidden {
     display: none;
   }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d39228f and 5e2a46c.

📒 Files selected for processing (6)
  • assets/index.less (1 hunks)
  • docs/api.md (2 hunks)
  • docs/demo/allow-clear.tsx (1 hunks)
  • docs/example.md (1 hunks)
  • src/InputNumber.tsx (5 hunks)
  • tests/allowClear.test.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • docs/demo/allow-clear.tsx
  • tests/allowClear.test.tsx
  • docs/example.md
  • docs/api.md
🧰 Additional context used
🧠 Learnings (2)
📚 Learning: 2024-09-29T06:18:11.993Z
Learnt from: bombillazo
Repo: react-component/input-number PR: 644
File: src/InputNumber.tsx:173-174
Timestamp: 2024-09-29T06:18:11.993Z
Learning: In `src/InputNumber.tsx`, within the `InternalInputNumber` component, the state variables `prevValueRef` and `inputValueRef` are typed as `string | number` to maintain consistency with existing code.

Applied to files:

  • src/InputNumber.tsx
📚 Learning: 2024-10-08T21:56:37.546Z
Learnt from: bombillazo
Repo: react-component/input-number PR: 644
File: src/InputNumber.tsx:393-395
Timestamp: 2024-10-08T21:56:37.546Z
Learning: The `InputNumber` component does not use error states or messages; it is designed to prevent the value from updating if validation fails without displaying additional feedback to the user.

Applied to files:

  • src/InputNumber.tsx
🔇 Additional comments (2)
src/InputNumber.tsx (2)

101-101: 属性类型定义合理

allowClear 支持布尔值或对象配置(带 clearValue),类型定义清晰且灵活。


120-120: 回调定义正确

onClear 回调的类型定义符合预期。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant